<template>
  <!-- 搜索 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item :label="t('systemUser.fields.nickname')" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          :placeholder="t('common.inputTextPlaceholder') + t('systemUser.fields.nickname')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item :label="t('systemUser.fields.jobCode')" prop="jobCode">
        <el-input
          v-model="queryParams.jobCode"
          :placeholder="t('common.inputTextPlaceholder') + t('systemUser.fields.jobCode')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" />
          {{ t('common.search') }}
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" />
          {{ t('common.reset') }}
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <ContentWrap>
    <vxe-table
      id="mentor-select-table"
      ref="tableRef"
      v-loading="loading"
      :data="list"
      stripe
      :column-config="{ resizable: true }"
      :size="vxeTableSize"
    >
      <vxe-column
        :title="t('systemUser.fields.nickname')"
        field="nickname"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('systemUser.fields.jobCode')"
        field="jobCode"
        align="center"
        show-header-overflow
        show-overflow
        min-width="75"
      />
      <vxe-column
        :title="t('systemUser.fields.mobile')"
        field="mobile"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('systemUser.fields.email')"
        field="email"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('systemUser.fields.identity')"
        field="identity"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('systemUser.fields.sex')"
        field="sex"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      >
        <template #default="scope">
          <dict-tag v-if="scope.row.sex" :type="DICT_TYPE.SYSTEM_USER_SEX" :value="scope.row.sex" />
        </template>
      </vxe-column>
      <vxe-column
        :title="t('systemUser.fields.zcm')"
        field="zcm"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('systemUser.fields.deptName')"
        field="deptName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('systemUser.fields.workingStatus')"
        field="workingStatus"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      >
        <template #default="scope">
          <dict-tag
            v-if="scope.row.workingStatus"
            :type="DICT_TYPE.WORKING_STATUS"
            :value="scope.row.workingStatus"
          />
        </template>
      </vxe-column>
      <vxe-column
        :title="t('common.operate')"
        field="operator"
        align="center"
        show-header-overflow
        show-overflow
        fixed="right"
        width="80"
      >
        <template #default="scope">
          <div class="flex items-center justify-center">
            <el-button
              type="primary"
              link
              @click="handleChoose(scope.row)"
              :disabled="scope.row.isMentor"
            >
              <Icon icon="ep:check" />
              {{ t('action.choose') }}
            </el-button>
          </div>
        </template>
      </vxe-column>
    </vxe-table>

    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>
<script lang="ts" setup>
import { DICT_TYPE } from '@/utils/dict'
import * as UserApi from '@/api/system/user'
import { UserVO } from '@/api/system/user'
import { useAppStore } from '@/store/modules/app'

defineOptions({ name: 'MentorSelectTable' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  nickname: undefined,
  jobCode: undefined,
  status: undefined,
  deptId: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await UserApi.getUserPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

const emits = defineEmits(['choose'])

const handleChoose = (row: UserVO) => {
  emits('choose', row)
}

/** 初始化 */
onMounted(() => {
  getList()
})
</script>
